package com.youdianstar.app.common.network



import android.view.View
import androidx.lifecycle.Observer
import com.youdianstar.app.common.event.NetEventLiveData
import com.youdianstar.app.common.loadsir.EmptyRecordCallback
import com.youdianstar.app.common.loadsir.ErrorCallback

import com.kingja.loadsir.callback.Callback
import com.kingja.loadsir.callback.SuccessCallback
import com.kingja.loadsir.core.Convertor
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.youdianstar.app.common.loadsir.EmptyCallback


/**
 * File: BaseApiResultObserver
 * Author: 82149 Create: 2021/11/1 17:11
 * Changes (from 2021/11/1)
 * --------------------------------------------------
 * description: 接口数据回调--更改UI(PagePaging + 列表)
 * ---------------------------------------------------
 *
 */
private const val TAG = "BaseApiPagingObserver"

abstract class BaseApiPagingObserver(view: View, isRecord:Boolean = false) : Observer<Int> , Callback.OnReloadListener {
    /*UI处理*/
    private var mLoadService: LoadService<Any>? = null

    init {
        mLoadService = LoadSir.getDefault().register(view, this,
            Convertor<Int> { t ->
                var resultCode: Class<out Callback> = SuccessCallback::class.java
                resultCode = when (t) {
                    //请求成功
                    NetEventLiveData.Result.RESULT_OK -> SuccessCallback::class.java
                    //数据为空
                    NetEventLiveData.Result.RESULT_EMPTY -> {
                        if (isRecord)
                            EmptyRecordCallback::class.java
                        else
                            EmptyCallback::class.java
                    }
                    NetEventLiveData.Result.RESULT_ERROR,NetEventLiveData.Result.NET_ERROR -> {
                        ErrorCallback::class.java
                    } else -> {
                        SuccessCallback::class.java
                    }
                }
                resultCode
            })
    }

    override fun onChanged(t: Int) {
        //处理UI :转换器回调
//        LogUtil.i("----NetPaging: $t")
        mLoadService?.showWithConvertor(t)
    }

}